package com.wwwarehouse.resource_center.fragment.createobject.creategoods;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewTreeObserver;

import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.tools.LogUtils;
import com.wwwarehouse.resource_center.R;
import com.wwwarehouse.resource_center.adapter.rules.AddCategoryAdapter;
import com.wwwarehouse.resource_center.bean.goods.CategoryBean;
import com.wwwarehouse.resource_center.fragment.rules.TheParentFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * Description:  添加类目
 * Copyright  : Copyright (c) 2017
 * Author     : yfwang
 * Date       : 2017/9/7 14:28
 */

public class AddCategotyFragment extends TheParentFragment {

    private RecyclerView mRvContent ;
    AddCategoryAdapter mAdapter;
    private List<CategoryBean.EmptyIdentifierBean> tagBeanList = new ArrayList<>();
    @Override
    protected int getContentId() {
        return R.layout.fragment_rule_select_application_objectst;
    }

    @Override
    public void onSuccess(CommonClass commonClass, int i) {
        LogUtils.showInfoLog("INSERT_CATEGORY:commonClass"+i);
    }

    @Override
    public void onReLoad(int code) {

    }

    @Override
    protected void init(View view) {
        mRvContent =  findView(view, R.id.rv_content);
    }
    public void setHeight(int height, int Width) {
        LogUtils.showInfoLog("height"+height+"Width"+Width);
        mAdapter = new AddCategoryAdapter(mActivity, tagBeanList, height,Width);
        mRvContent.setAdapter(mAdapter);
    }
    @Override
    protected void loadDatas() {
        Bundle b = getArguments();
        List<CategoryBean.EmptyIdentifierBean> taskUnits  = b.getParcelableArrayList("data");
        int pos = b.getInt("pos");
        int num = b.getInt("num");
        for (int i = pos * num; i < num + pos * num; i++) {
            if (i >= 0 && i < taskUnits.size()) {
                tagBeanList.add(taskUnits.get(i));
            }
        }
        mRvContent.setLayoutManager(new GridLayoutManager(mActivity,4){
            @Override
            public boolean canScrollHorizontally() {
                return false;
            }
            @Override
            public boolean canScrollVertically() {
                return false;
            }

        });
        final ViewTreeObserver observer = mRvContent.getViewTreeObserver();
        observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                observer.removeGlobalOnLayoutListener(this);
                setHeight(mRvContent.getHeight(),mRvContent.getWidth());
            }
        });
        if (mAdapter != null) {
            mRvContent.setAdapter(mAdapter);
        }
    }

}
